<template>
  <view class="phxx-item-detail">
    <view class="content-box">
      <view class="content-list">
        <view class="item" v-for="(item, index) in priceDatas">
          <price-item :priceStatusObj="priceStatusObj" :showValidBtn="showValidBtn" :showERP="showErpFlag" :itemList="item"
            @handlFun="handlFun" @manualCalling="handleFun"></price-item>
        </view>
        <uni-load-more :status="pageParams.isMore" />
      </view>
      <view v-if="hasPermission && showErpFlag">
        <view class="btns">
          <button type="primary" @click="handleClick">添加</button>
        </view>
      </view>

      <uni-popup ref="searchPopup">
        <view class="pop violation-pop">
          <view class="phxx-itemss">
            <view class="header">
              <view class="title">添加</view>
            </view>
            <view class="top calling">
              <view class="infor-box">
                <uni-forms-item class="label" label="产品价格" required name="freight">
                  <input type="text" v-model="formData.freight" class="widt328" placeholder="请输入产品价格"></input><label
                    style="width:auto;">元/吨</label>
                </uni-forms-item>
                <uni-forms-item class="label" label="执行时间" required name="executionTime">
                  <w-picker :visible="poundListFlag" mode="date" startYear="1900" endYear="2124" :value="curDateTime"
                    :current="false" fields="minute" @confirm="onConfirm($event,'date1')" @cancel="onCancel"
                    :disabled-after="false" ref="date1"></w-picker>
                  <input class="disab-box" v-model="formData.executionTime" disabled placeholder="请选择执行时间"
                    @click="poundListFlag=true"></input>
                </uni-forms-item>

              </view>
            </view>
            <view class="btn">
              <button class="mini-btn" @click="closePopup" type="primary" plain="true" size="mini">取消</button>
              <button class="mini-btn" @click="handleUpdateClick" type="primary" plain="true" size="mini">确定</button>
            </view>
          </view>
        </view>
      </uni-popup>
    </view>
  </view>
</template>

<script>
  import {
    toast, getButtonShow
  } from '@/utils/common';
  import {
    adjustPricePages,
    createAdjustprice,
    getEnterBasic
  } from '@/api/tranplan.js';
  import {
    getDriverId, getTenantId
  } from '@/utils/auth'
  import {
    dictTansParams,
    getDateFormat
  } from '@/utils/common.js';
  import { getDictDatas } from '@/api/transport.js';
  export default {
    data() {
      return {
        showErpFlag: false,
        priceDatas: [],
        hasPermission: false,
        showValidBtn: false,
        poundListFlag: false,
        itemList: {
          purchaseId: undefined,
          purchaser: undefined,
        },
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          transportPlanId: undefined,
          type: 2,
        },
        formData: {
          transportPlanId: undefined,
          freight: undefined,
          executionTime: undefined,
          type: 2
        },
        pageParams: {
          isMore: 'more'
        },
        priceStatusObj: {},
        appointmentPriceTypeList: [],
        appointmentPriceType: undefined,
        dictObj: {}
      };
    },
    async onLoad(options) {
      //获取字典
      this.getLicenseTypeDict()
      //获取字典
      if (!!options) {
        if (!!options.id) {
          this.queryParams.transportPlanId = options.id
          this.getLists()
        }
      }
    },
    async onShow() {
      if (getTenantId() && getDriverId()) {
        this.hasPermission = await getButtonShow("plan:datePrice:add");
        this.showValidBtn = await getButtonShow("plan:datePrice:invalid");
      }
      let basicConfig = await getEnterBasic(getTenantId())
      if (basicConfig.code == 200) {
        this.showErpFlag = basicConfig.data?.erpDocking == 0 ? false : true
      }
    },
    onPullDownRefresh() {
      this.getLicenseTypeDict()
      this.queryParams.pageNo == 1
      this.getLists()
    },
    onReachBottom() {
      this.getLicenseTypeDict()
      if (this.pageParams.isMore == 'more') {
        this.queryParams.pageNo++
        this.getLists()
      }
    },
    methods: {
      handleFun() {
        this.getLists()
      },
      closePopup() {
        this.$refs['searchPopup'].close();
      },
      async getLicenseTypeDict() {
        let datas = await getDictDatas()
        let priceStatusList = dictTansParams(datas.data, 'price_adjustment_status', 1)
        priceStatusList.forEach((item) => {
          this.priceStatusObj[item.value] = item.text;
        });
        this.appointmentPriceTypeList = dictTansParams(datas.data, 'appointment_price_adjustment_type', 1)
        if (this.appointmentPriceTypeList.length > 0) {
          this.appointmentPriceType = this.appointmentPriceTypeList[0].value
          // this.queryParams.type = this.appointmentPriceType
        }
      },
      async handleUpdateClick() {
        if (!this.formData.freight) {
          toast("请输入正确运价")
          return false
        } else {
          if (this.formData.freight < 0 || this.formData.freight > 999999.00) {
            toast("请输入正确运价")
            return false
          }
        }
        if (!this.formData.executionTime) {
          toast("请输入执行时间")
          return false
        } else {
          let currtTime = new Date().getTime()
          if (this.formData.executionTime < currtTime) {
            toast("预约时间不能早于当前时间")
            return false
          }
        }

        this.formData.transportPlanId = this.queryParams.transportPlanId
        let datas = {
          transportPlanId: this.formData.transportPlanId,
          freight: this.formData.freight,
          executionTime: this.formData.executionTime + ":00",
          type: 2
        }
        let res = await createAdjustprice(datas)
        if (res.code == 0) {
          toast("添加成功")
          this.getLists()
          this.$refs['searchPopup'].close();
        } else {
          toast(res.msg)
        }
      },
      async getLists() {
        try {
          let res = await adjustPricePages(this.queryParams)
          if (res.code == 0) {
            let _ = this
            if (this.queryParams.pageNo == 1) {
              this.priceDatas = res.data.list
            } else {
              this.priceDatas = [...this.priceDatas, ...res.data.list];
            }

            this.priceDatas = this.priceDatas.map(item => {
              if (!!item.endTime) {
                (item['etime'] = getDateFormat(item.endTime))
              }
              if (!!item.executionTime) {
                (item['exetime'] = getDateFormat(item.executionTime))
              }
              // _.priceStatusList.forEach(k => {
              //   if (k.value == item.status) {
              //     item['statusName'] = k.text
              //   }
              // })
              return item
            })
            if (res.data.total == this.priceDatas.length) {
              this.pageParams.isMore = 'noMore';
            } else {
              this.pageParams.isMore = 'more';
            }
          }
          wx.stopPullDownRefresh();
        } catch (e) {
          return false;
        }
      },
      handleClick() {
        this.formData = {
          freight: undefined,
          executionTime: undefined,
        }
        this.$refs['searchPopup'].open();
      },
      onConfirm(res) {
        let times = res.value.split(" ")[1].split(":")
        this.formData.executionTime = res.value.split(" ")[0] + " " + times[0] + ":" + times[1]
        this.poundListFlag = false
      },
      async onCancel() {
        this.poundListFlag = false
      }
    }
  };
</script>

<style lang="scss" scoped>
  .phxx-item-detail {
    padding-bottom: 163rpx;
  }

  .phxx-item {
    width: 700rpx;
    padding: 10rpx 30rpx;
    box-sizing: border-box;
    margin: 20rpx auto 20rpx auto;
    border-radius: 20rpx;
    background: #fff;
    font-size: 26.66rpx;
    color: #000;

    .phxx-item-content {
      width: 644rpx;

      .bottom {
        .title-box {
          padding: 30rpx 0 0 0;
          font-size: 30.22rpx;
        }

        >.items {
          padding-bottom: 4rpx;
          display: flex;
          align-items: center;
          border-bottom: 1px solid #ccc;
          padding-top: 15px;

          .net-selct-box {
            width: 77%;
          }
        }

        .btn {
          button {
            width: 100%;
            height: 40px !important;
            margin-top: 45px;
            line-height: 40px !important;
          }
        }

        .button {
          flex: 1;
          /* 将按钮的宽度均分 */
          height: 40px !important;
          line-height: 40px !important;
        }

        .cancel {
          margin-right: 10px;
          /* 设置取消按钮的右边距 */
        }

        .update {
          margin-left: 10px;
          /* 设置更新按钮的左边距 */
        }

        .til {
          width: 220rpx;
          display: inline-block;
        }

        .val {
          border: 1px solid #ccc;
          margin-left: 30rpx;
          width: 80%;
          display: inline-block;
          border-radius: 5px;
          height: 35px;
        }

        .searchPopup {
          position: absolute;
          right: 0;

          .up-searcbox {
            background: #fff;
            width: 800rpx;
            position: absolute;
            right: -195px;
            height: 30vh;
            padding: 30px;
            top: -200px;

            .search-header {
              .items {
                margin-bottom: 10px;

                input {
                  border: 1px solid #ccc;
                  border-radius: 3px;
                  height: 40px;
                  margin-top: 10px;
                  padding: 0 2px;
                  font-size: 24.88rpx;
                }
              }
            }

            .search-footer {
              bottom: 20%;
              position: absolute;
              display: flex;
              align-content: center;
              flex-flow: nowrap;

              button {
                width: 150px;
                float: right;
                font-size: 21.33rpx !important;
                border: 1px solid #409eff !important;
              }

              button:nth-child(2) {
                margin-left: 30px;
              }
            }
          }
        }
      }

      .demonstration {
        display: block;
        color: var(--el-text-color-secondary);
        font-size: 24.88rpx;
        margin-bottom: 20px;
        width: 100px;
      }
    }
  }

  .btns {
    position: fixed;
    bottom: 0;
    height: 150rpx;
    width: 100%;
    margin: auto;
    z-index: 99;
    left: 0;
    right: 0;
    padding: 20rpx;
    background-color: #fff;
  }

  ::v-deep.phxx-itemss {
    .top {
      .uni-forms-item {
        .uni-forms-item__content {
          position: relative;
          font-size: 14px;
          flex: 1;
          box-sizing: border-box;
          flex-direction: row;
          width: auto !important;

          label {
            position: relative;
            top: -26rpx;
            left: 4rpx;
          }
        }

        input {
          height: 70rpx;
          line-height: 70rpx;
          background: #fff;
          border: 1px solid #DCDFE6;
          border-radius: 6rpx;
          font-size: 23.33rpx;
          text-indent: 10rpx;
          width: 388rpx;
          display: inline-block;
          padding: 0 8rpx;
        }

        input.widt328 {
          width: 326rpx;
        }
      }
    }
  }
</style>